package com.jiayi.weiphoto.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiayi.weiphoto.R;
import com.jiayi.weiphoto.entity.HDEntity;
import com.jiayi.weiphoto.net.Util;
import com.jiayi.weiphoto.page.FixedWidthImageView;
import com.jiayi.weiphoto.util.SearchSyncImageLoader;
import com.jiayi.weiphoto.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class HdListAdapter extends BaseAdapter {

	// List<Map<String, String>> hd=null;
	ArrayList<HDEntity> hdList;
	Context context = null;
	// private ImageLoader mImageLoader;
	// private boolean mBusy = false;

	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	// protected ImageLoader imageLoader = ImageLoader.getInstance();
	// DisplayImageOptions options;
	SearchSyncImageLoader imageLoader;
	ListView parentView;

	public HdListAdapter(Context context, List<Map<String, String>> hd,
			ListView parentViews) {
		// this.hd=hd;
		this.context = context;
		imageLoader = SearchSyncImageLoader.getInstance();
		imageLoader.setCacheRootPath(StringUtil.genCompanyRootPath(context));
		this.parentView = parentViews;
		// mImageLoader = new ImageLoader(context);
		// options = new DisplayImageOptions.Builder()
		// .cacheInMemory()
		// .cacheOnDisc()
		// .build();
	}

	public HdListAdapter(Context context, ArrayList<HDEntity> hdList,
			ListView lv_company) {
		// TODO Auto-generated constructor stub
		this.hdList = hdList;
		this.context = context;
		imageLoader = SearchSyncImageLoader.getInstance();
		imageLoader.setCacheRootPath(StringUtil.genCompanyRootPath(context));
		this.parentView = lv_company;
	}

	@Override
	public int getCount() {
		return hdList.size();
	}

	// public ImageLoader getImageLoader() {
	// return mImageLoader;
	// }

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return hdList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;

		if (convertView == null) {
			holder = new Holder();
			convertView = LayoutInflater.from(context).inflate(R.layout.hd_item, null);
			holder.iv_yp_item = (FixedWidthImageView) convertView.findViewById(R.id.iv_hd_item);
			holder.tv_hd_imte_title = (TextView) convertView.findViewById(R.id.txt_hditem_title);
			holder.leftDay = (TextView) convertView.findViewById(R.id.txt_hditem_left);
			holder.hdPart = (RelativeLayout) convertView.findViewById(R.id.hd_leftday_part);
			convertView.setTag(holder);
		}else{
			holder = (Holder) convertView.getTag();
		}
		holder.tv_hd_imte_title.setText(hdList.get(position).getTitle());
		String leftDay = Util.getLeftDay(hdList.get(position).getBeginTime(), hdList.get(position).getEndTime());
		if(leftDay.length() == 0){
			holder.hdPart.setVisibility(View.GONE);
		}else{
			holder.hdPart.setVisibility(View.VISIBLE);
			holder.leftDay.setText(leftDay);
		}
		String url = MainConstants.HOST_img + hdList.get(position).getPic();
		holder.iv_yp_item.setTag(url);
		if (hdList.get(position).getPic() == null
				|| hdList.get(position).getPic().length() == 0) {

		} else {
			if (hdList.get(position).getCovert() == null) {
				imageLoader.loadImage(position, url,
						new SearchSyncImageLoader.OnImageLoadListener() {

							@Override
							public void onImageLoad(String iconUrl,
									Bitmap bitmap, int pos) {
								// TODO Auto-generated method stub
								FixedWidthImageView imgView = (FixedWidthImageView) parentView
										.findViewWithTag(iconUrl);
								if (imgView != null) {
									imgView.setImageBitmap(bitmap);
								}
								hdList.get(pos).setCovert(bitmap);
							}

							@Override
							public void onError(Integer t) {
								// TODO Auto-generated method stub

							}
						});
			} else {
				Bitmap bitmap = hdList.get(position).getCovert();
				// holder.iv_yp_item.setImageBitmap(bitmap);
				holder.iv_yp_item.setImageBitmap(bitmap);
			}
		}
		return convertView;
	}

	int itemWidth = -1;

	static class Holder {
		// FixedWidthImageView iv_yp_item;
		FixedWidthImageView iv_yp_item;
		TextView tv_hd_imte_title;
		TextView leftDay;
		RelativeLayout hdPart;
		// TextView tv_hd_item_con;
		// TextView tv_hd_item_bt;
		// TextView tv_hd_item_et;
	}

	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			// if (loadedImage != null) {
			// ImageView imageView = (ImageView) view;
			// boolean firstDisplay = !displayedImages.contains(imageUri);
			// if (firstDisplay) {
			// FadeInBitmapDisplayer.animate(imageView, 500);
			// displayedImages.add(imageUri);
			// } else {
			// imageView.setImageBitmap(loadedImage);
			// }
			// }
		}
	}

}
